<%-- 
    Document   : manteenfermedad
    Created on : Aug 19, 2013, 11:08:07 AM
    Author     : IK
--%>


<%@page import="edu.capex.farmacus.Manager.EnfermedadesManager"%>
<%@page import="edu.capex.farmacus.encapsulates.Enfermedades"%>
<%@page import="edu.capex.farmacus.Manager.MedicamentosManager"%>
<%@page import="edu.capex.farmacus.encapsulates.Medicamentos"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
    Enfermedades enfermedadAMod = null;

    if (request.getParameter("nombre") != null && request.getParameter("sintoma") != null
            && request.getParameter("medicamento") != null
            && request.getParameter("id") != null && request.getParameter("moid") == null) {

        //Enfermedades enfermedad = new Enfermedades(new Integer(request.getParameter("id")));
        Enfermedades enfermedad = new EnfermedadesManager().findEnfermedades(new Integer(request.getParameter("id")));
        boolean existe = false;

        if (enfermedad == null) {
            enfermedad = new Enfermedades();
        } else {
            existe = true;
        }

        enfermedad.setIdEnfermedades(new Integer(request.getParameter("id")));
        enfermedad.setNombreEnfermedad(request.getParameter("nombre"));
        enfermedad.setSintomas(request.getParameter("sintoma"));
        enfermedad.setMedicamento(new MedicamentosManager().findMedicamentos(new Integer(request.getParameter("medicamento"))));

        if (existe) {
            new EnfermedadesManager().edit(enfermedad);
        } else {
            new EnfermedadesManager().create(enfermedad);
        }

    } else if (request.getParameter("moid") != null && request.getParameter("modificar").equalsIgnoreCase("modificar")) {
        enfermedadAMod = new EnfermedadesManager().findEnfermedades(new Integer(request.getParameter("moid")));
    } else if (request.getParameter("reid") != null && request.getParameter("eliminar").equalsIgnoreCase("eliminar")) {
        new EnfermedadesManager().destroy(new Integer(request.getParameter("reid")));
    }

%>
<html>
    <head>
        <%@include file="/resources/header.jsp" %>
        <script src="../js/bsa.js"></script>
    </head>
    <%@include file="/resources/header1.jsp" %>
    <body class="preview" data-spy="scroll" data-target=".subnav" data-offset="80">

        <div class="container">
            <!-- Typography
            ================================================== -->
            <section id="typography">
                
                <%@include file="/admin/supervisores/menuadm.jsp" %>

                <h2>Mantenimiento Enfermedades </h2>

                <section id="forms">

                    <form class="form-horizontal well" method="POST" id="myForm">
                        <fieldset>

                            <%if (enfermedadAMod != null) {%>

                            <input type="hidden" name="id" value="<%=enfermedadAMod.getIdEnfermedades()%>"/>
                            <div class="control-group">
                                <label class="control-label" for="input01">Nombre</label>
                                <div class="controls">
                                    <input type="text" class="input-xlarge" id="input01" data-required name="nombre" value="<%=enfermedadAMod.getNombreEnfermedad()%>">
                                </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="input01">Sintomas</label>
                                <div class="controls">
                                    <input type="text" class="input-xlarge" id="input01" data-required name="sintoma" value="<%=enfermedadAMod.getSintomas()%>">
                                </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="input01">Medicamentos</label>
                                <div class="controls">
                                    <select name="medicamento" class="input-xlarge">
                                        <%
                                            for (Medicamentos temp : new MedicamentosManager().findMedicamentosEntities()) {
                                                if (enfermedadAMod.getMedicamento().equals(temp)) {
                                        %>
                                        <option value="<%=temp.getIdmedicamentos()%>" selected><%=temp.getNombreMedicamento()%></option>
                                        <%} else {%>
                                        <option value="<%=temp.getIdmedicamentos()%>"><%=temp.getNombreMedicamento()%></option>
                                        <%}%>

                                        <%}%>
                                    </select>
                                </div>
                            </div>


                            <div class="form-actions">
                                <button type="submit" class="btn btn-primary" name="modificar">Modificar</button>
                                <button type="reset" class="btn">Cancelar</button>
                            </div>

                            <%} else {%>

                            <input type="hidden" name="id" value="<%=new EnfermedadesManager().nextId()%>"/>
                            <div class="control-group">
                                <label class="control-label" for="input01">Nombre</label>
                                <div class="controls">
                                    <input type="text" class="input-xlarge" id="input01" data-required name="nombre">
                                </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="input01">Sintomas</label>
                                <div class="controls">
                                    <input type="text" class="input-xlarge" id="input01" data-required name="sintoma">
                                </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="input01">Medicamentos</label>
                                <div class="controls">
                                    <select name="medicamento" class="input-xlarge">
                                        <%
                                            for (Medicamentos temp : new MedicamentosManager().findMedicamentosEntities()) {
                                        %>
                                        <option value="<%=temp.getIdmedicamentos()%>"><%=temp.getNombreMedicamento()%></option>
                                        <%}%>
                                    </select>
                                </div>
                            </div>


                            <div class="form-actions">
                                <button type="submit" class="btn btn-primary">Agregar</button>
                                <button type="reset" class="btn">Cancelar</button>
                            </div>
                            <%}%>


                        </fieldset>
                    </form>
                    <table id="myTable" class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <th>
                                    Nombre
                                </th>
                                <th>
                                    Sintoma
                                </th>
                                <th filter-type="ddl">
                                    Medicamento
                                </th>
                                <th filter="false">
                                    Modificar
                                </th>
                                <th filter="false">
                                    Eliminar
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <%
                                for (Enfermedades temp : new EnfermedadesManager().findEnfermedadesEntities()) {
                            %>
                            <tr>
                                <td>
                                    <%=temp.getNombreEnfermedad()%>
                                </td>
                                <td>
                                    <%=temp.getSintomas()%>
                                </td>
                                <td>
                                    <%=temp.getMedicamento().getNombreMedicamento()%>
                                </td>
                                <td>
                                    <form action="#" method="POST">
                                        <center>
                                            <input type="hidden" name="moid" value="<%=temp.getIdEnfermedades()%>"/>
                                            <input type="submit" name="modificar" value="Modificar" class="btn"/>
                                        </center>
                                    </form>
                                </td>
                                <td>
                                    <form action="#" method="POST">
                                        <center>
                                            <input type="hidden" name="reid" value="<%=temp.getIdEnfermedades()%>"/>
                                            <input type="submit" name="eliminar" value="Eliminar" class="btn"/>
                                        </center>
                                    </form>
                                </td>
                            </tr>
                            <%}%>
                        </tbody>
                    </table>

                    <%@include file="/resources/footer1.jsp" %>
                </section>
        </div>
        <script type="text/javascript">
            $(document).ready(function() {
                $("#myTable").tableFilter();
                $("#fecha").datepicker();
                $('#myForm').validate({
                    onKeyup: true,
                    eachValidField: function() {

                        $(this).closest('div').removeClass('error').addClass('success');
                    },
                    eachInvalidField: function() {

                        $(this).closest('div').removeClass('success').addClass('error');
                    }
                });

            });
        </script>
    </body>
</html>

